package com.ykdl.member.kid.doctor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.DoctorOnlineBean;
import com.ykdl.member.kid.beans.DoctorOnlineListBean;
import com.ykdl.member.kid.beans.DoctorsBean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 医生坐诊列表Acivity
 * @author chenshuai
 */
public class DoctorOnlineListActivity extends BaseScreen{
	int[] tes={1,2,3,3,3,3};
	private  RefreshListView doctor_list ;
	private DoctorOnlineAdatapr adatapr ;
	private BitmapUtils bitmapUtil ;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 40;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("专家坐诊", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.doctoronlinelistactivity_xml);
		bitmapUtil = new BitmapUtils(this);
		inint();
	}
	public void inint() {
		doctor_list = (RefreshListView)findViewById(R.id.doctor_list);
		adatapr = new DoctorOnlineAdatapr();
		doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				DoctorOnlineBean bean = (DoctorOnlineBean)adatapr.getItem(arg2-1);
				Intent intent =null;
				if (bean.getStatus()==0) {
					intent = new Intent(DoctorOnlineListActivity.this,CampaignInfoWebView.class);
					intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
					DoctorOnlineListActivity.this.startActivity(intent);
				}else if(bean.getStatus()==1){
					intent = new Intent(DoctorOnlineListActivity.this,ActionsDoctorOnlineActivity.class);
					intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
					DoctorOnlineListActivity.this.startActivity(intent);
				}else if(bean.getStatus()==2){
					intent = new Intent(DoctorOnlineListActivity.this,ActionsDoctorOnlineActivity.class);
					intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
					intent.putExtra(KidAction.IS_GONE_BT, false);
					DoctorOnlineListActivity.this.startActivity(intent);
				}
				
				
			}
		});
		doctor_list.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				cursor = 0;
				inintdata();
			}
			});
		doctor_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				inintdata();
			}});
		inintdata();
	}
	public void inintdata() {
		showProgress("正在发送请稍等...");
		String  pageurl =KidConfig.GET_ONLINE_DOCTORLIST;
		Map<String,String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl);
		 TaskManager.startHttpRequest(request, new getDoctorOnlineListTag(),  DoctorOnlineListBean.class);
	}
	
	class getDoctorOnlineListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof DoctorOnlineListBean) {
				DoctorOnlineListBean bean = (DoctorOnlineListBean)t;
				if (bean.getList()!=null) {
					hidErrorTip( );
					if (bean.getNext_cursor() == bean.getTotal_number()) {
						doctor_list.setMoreButtoIsGon(true);
					} else {
						doctor_list.setMoreButtoIsGon(false);
						cursor = bean.getNext_cursor();
					}
					adatapr.setList(bean.getList());				
					doctor_list.setAdapter(adatapr);
					adatapr.notifyDataSetChanged();
					doctor_list.onRefreshComplete();
					doctor_list.setFecthMoreOk();
				}else {
					showErrorTip("没有数据...", "");
					doctor_list.setMoreButtoIsGon(true);
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	class DoctorOnlineAdatapr extends BaseAdapter {
		private ArrayList<DoctorOnlineBean>  list;
		public void setList(ArrayList<DoctorOnlineBean>  list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list != null) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			if (getCount() != 0) {
				return list.get(position);
			} else {
				return null;
			}
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			HolderView holderView;
			if (convertView == null) {
				holderView = new HolderView();
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.doctoronline_item_xml, null);
				holderView.doctor_head = (ImageView) convertView.findViewById(R.id.doctor_head);
				holderView.new_icon = (ImageView) convertView.findViewById(R.id.new_icon);
				holderView.doctor_name = (TextView) convertView.findViewById(R.id.doctor_name);
				holderView.doctor_position = (TextView) convertView.findViewById(R.id.doctor_position);
				holderView.doctor_hospital = (TextView) convertView.findViewById(R.id.doctor_hospital);
				holderView.doctor_adrr = (TextView) convertView.findViewById(R.id.doctor_adrr);
				holderView.doctor_subject = (TextView) convertView.findViewById(R.id.doctor_subject);
				holderView.doctor_onlinetime = (TextView) convertView.findViewById(R.id.doctor_onlinetime);
				convertView.setTag(holderView);
				}else {
					holderView = (HolderView) convertView.getTag();
				}
			if (list.get(position).getDoctor()!=null) {
				if (list.get(position).getDoctor().getAvatar_file_meta()!=null) {
					String url = list.get(position).getDoctor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
					bitmapUtil.display(holderView.doctor_head, url);
				}
				holderView.doctor_name.setText(list.get(position).getDoctor().getDisplay_name());
				if (list.get(position).getDoctor().getProfile()!=null) {
					holderView.doctor_position.setText(list.get(position).getDoctor().getProfile().getTitle()+"/"+list.get(position).getDoctor().getProfile().getDepartment());
					holderView.doctor_hospital.setText(list.get(position).getDoctor().getProfile().getInstruction());
					holderView.doctor_adrr.setText(list.get(position).getDoctor().getProfile().getDepartment());
				}
				
			}
			if (list.get(position).getStatus()==0) {
				holderView.new_icon.setVisibility(View.VISIBLE);
			}else {
				holderView.new_icon.setVisibility(View.INVISIBLE);
			}
			holderView.doctor_subject.setText("主题："+list.get(position).getTitle());
			if (list.get(position).getDoctor()!=null) {
				holderView.doctor_head.setTag(list.get(position));	
			}
			holderView.doctor_onlinetime.setText("在线时间："+DateUtil.getDateDatatimeSting(list.get(position).getStart_time())+"-"+DateUtil.getDateDatatimeSting(list.get(position).getEnd_time()));
			holderView.doctor_head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					DoctorOnlineBean bean = (DoctorOnlineBean)v.getTag();
					Intent intent =null;
					if (bean.getStatus()==0) {
						intent = new Intent(DoctorOnlineListActivity.this,CampaignInfoWebView.class);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
						DoctorOnlineListActivity.this.startActivity(intent);
					}else if(bean.getStatus()==1){
						intent = new Intent(DoctorOnlineListActivity.this,ActionsDoctorOnlineActivity.class);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
						DoctorOnlineListActivity.this.startActivity(intent);
					}else if(bean.getStatus()==2){
						intent = new Intent(DoctorOnlineListActivity.this,ActionsDoctorOnlineActivity.class);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, bean.getCampaign_id()+"");
						intent.putExtra(KidAction.IS_GONE_BT, false);
						DoctorOnlineListActivity.this.startActivity(intent);
					}
					
				}
			});
			
			return convertView;
		}
		class HolderView{
			TextView doctor_name ;
			TextView doctor_position  ;
			TextView doctor_hospital  ;
			TextView doctor_adrr  ;
			TextView doctor_subject  ;
			TextView doctor_onlinetime  ;
			ImageView doctor_head ,new_icon;
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	public int isintime(String start,String end) {
		int isintime = 0;
			long longtime = Long.parseLong(start);
			Date starttime = new Date(longtime);
			long longtime1 = Long.parseLong(end);
			Date endtime = new Date(longtime1);
			Date nowtime = new Date();
			if (nowtime.before(endtime)&&nowtime.after(starttime)) {
				//活动进行中
				isintime = 1 ;
			}else if (nowtime.before(starttime)){
				//活动未开始
				isintime = 2 ;
			}else {
				//活动结束
				isintime = 3 ;
			}
		return isintime;
		
		
	}
}
